<template>
    <div>
        <div class="crumbs">
            <el-breadcrumb separator="/">
                <el-breadcrumb-item>
                    <i class="el-icon-lx-cascades"></i> 消息管理
                </el-breadcrumb-item>
            </el-breadcrumb>
        </div>
        <div class="container">
            <div class="handle-box">
                <el-button type="danger" icon="el-icon-circle-plus" @click="handleAdd">新增消息</el-button>
            </div>
            <el-table
                :data="tableData"
                border
                class="table"
                ref="multipleTable"
                header-cell-class-name="table-header"
            >
                <el-table-column label="序号" width="55" align="center">
                    <template slot-scope="scope">
                        {{scope.$index + 1}}
                    </template>
                </el-table-column>
                <el-table-column prop="content" label="消息内容"></el-table-column>
                <el-table-column label="创建日期">
                    <template slot-scope="scope">{{formatDate(scope.row.create_time)}}</template>
                </el-table-column>
                <el-table-column label="结束日期">
                    <template slot-scope="scope">{{formatDate(scope.row.end_time)}}</template>
                </el-table-column>
                <el-table-column label="是否推送">
                    <template slot-scope="scope">
                        <el-switch :value="getIsPublish(scope.row.is_publish)"></el-switch>
                    </template>
                </el-table-column>
                <el-table-column label="操作" width="180" align="center">
                    <template slot-scope="scope">
                        <el-button
                            type="text"
                            icon="el-icon-edit"
                            @click="handleEdit(scope.$index, scope.row)"
                        >编辑</el-button>
                        <el-button
                            type="text"
                            icon="el-icon-delete"
                            class="red"
                            @click="handleDelete(scope.$index, scope.row)"
                        >删除</el-button>
                    </template>
                </el-table-column>
            </el-table>
            <div class="pagination">
                <el-pagination
                    background
                    layout="total, prev, pager, next"
                    :current-page="query.pageNum"
                    :page-size="query.pageSize"
                    :total="pageTotal"
                    @current-change="handlePageChange"
                ></el-pagination>
            </div>
        </div>

        <!-- 编辑弹出框 -->
        <el-dialog title="编辑" :visible.sync="editVisible" width="30%">
            <el-form ref="form" :model="form" label-width="70px">
                <el-form-item label="消息内容">
                    <el-input v-model="form.content"></el-input>
                </el-form-item>
                <el-form-item label="截至日期">
                    <el-date-picker
                    v-model="form.end_time"
                    type="date"
                    value-format="timestamp"
                    placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="是否推送">
                    <el-switch v-model="form.is_publish" active-text="推送" inactive-text="不推送"></el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="editVisible = false">取 消</el-button>
                <el-button type="primary" @click="saveEdit">确 定</el-button>
            </span>
        </el-dialog>


        <!-- 新增弹出框 -->
        <el-dialog title="新增" :visible.sync="addVisible" width="30%">
            <el-form ref="form" :model="message" label-width="80px">
                <el-form-item label="消息内容">
                    <el-input v-model="message.content"></el-input>
                </el-form-item>
                <el-form-item label="截至日期">
                    <el-date-picker
                    v-model="message.end_time"
                    type="date"
                    value-format="timestamp"
                    placeholder="选择日期">
                    </el-date-picker>
                </el-form-item>
                <el-form-item label="是否推送">
                    <el-switch v-model="message.is_publish" active-text="推送" inactive-text="不推送"></el-switch>
                </el-form-item>
            </el-form>
            <span slot="footer" class="dialog-footer">
                <el-button @click="addVisible = false">取 消</el-button>
                <el-button type="primary" @click="submit">确 定</el-button>
            </span>
        </el-dialog>
    </div>
</template>

<script>
import { createMessage,getMessage,updateMessage,delMessage } from '../../api/message';
export default {
    name: 'basetable',
    data() {
        return {
            query: {
                pageNum: 1,
                pageSize: 5
            },
            message: {
                content: '',
                end_time: '',
                is_publish: true
            },
            tableData: [],
            multipleSelection: [],
            delList: [],
            editVisible: false,
            addVisible: false,
            pageTotal: 0,
            form: {},
            idx: -1,
            id: -1
        };
    },
    created() {
        this.getData();
    },
    methods: {
        getIsPublish(publish){
            if(publish == 1){
                return true;
            }else{
                return false;
            }
        },
        formatDate(time) {
            var date = new Date(time);
            var year = date.getFullYear();
            var month = date.getMonth() + 1;
            var day = date.getDate();
            return year + '年' + month + '月' + day + '日';
        },
        // 获取 easy-mock 的模拟数据
        getData() {
            getMessage(this.query).then(res => {
                console.log(res);
                this.tableData = res.data.list;
                this.pageTotal = res.data.total;
            })
        },
        // 触发搜索按钮
        handleSearch() {
            this.$set(this.query, 'pageIndex', 1);
            this.getData();
        },
        // 删除操作
        handleDelete(index, row) {
            // 二次确认删除
            this.$confirm('确定要删除吗？', '提示', {
                type: 'warning'
            })
                .then(() => {
                    var param = {
                        id: this.tableData[index].id
                    }
                    delMessage(param).then(res => {
                        if(res.code == 200){
                            this.$message.success('删除成功');
                        }else{
                            this.$message.success('删除失败');
                        }
                        this.getData();
                    })
                })
                .catch(() => {});
        },
        // 编辑操作
        handleEdit(index, row) {
            this.idx = index;
            this.form = JSON.parse(JSON.stringify(row));
            if(this.form.is_publish == 1){
                this.form.is_publish = true;
            }else{
                this.form.is_publish = false;
            }
            this.editVisible = true;
        },
        // 保存编辑
        saveEdit() {
            var is_publish = 1;
            if(this.form.is_publish === false){
                is_publish = 0;
            }
            var param = {
                id: this.tableData[this.idx].id,
                content: this.form.content,
                end_time: this.form.end_time,
                is_publish: is_publish
            }
            updateMessage(param).then(res => {
                if(res.code == 200){
                    this.$message.success("消息修改成功");
                    this.$message.success(`修改第 ${this.idx + 1} 行成功`);
                    this.getData();
                }else{
                    this.$message.error("消息修改失败");
                }
            })
            this.editVisible = false;
            this.form = {};
        },
        // 分页导航
        handlePageChange(val) {
            this.$set(this.query, 'pageNum', val);
            this.getData();
        },
        handleAdd() {
            this.addVisible = true;
        },
        submit() {
            console.log(this.message);
            var is_publish = 1;
            if(this.message.is_publish === false){
                is_publish = 0;
            }
            var param = {
                content: this.message.content,
                end_time: this.message.end_time,
                is_publish: is_publish
            }
            createMessage(param).then(res => {
                if(res.code == 200){
                    this.$message.success("消息添加成功");
                }else{
                    this.$message.error("消息添加失败");
                }
            })
            this.addVisible = false;
            this.message = {
                content: '',
                end_time: '',
                is_publish: true
            }
        }
    }
};
</script>

<style scoped>
.handle-box {
    margin-bottom: 20px;
}

.handle-select {
    width: 120px;
}

.handle-input {
    width: 300px;
    display: inline-block;
}
.table {
    width: 100%;
    font-size: 14px;
}
.red {
    color: #ff0000;
}
.mr10 {
    margin-right: 10px;
}
.table-td-thumb {
    display: block;
    margin: auto;
    width: 40px;
    height: 40px;
}
</style>
